<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <treeTools :tree-node="company" :is-root="true" @addDepartment="addDepartment" />
        <hr>
        <!-- 用了一个行列布局 显示公司信息-->
        <el-tree
          :data="depts"
          :props="{label: 'name'}"
          default-expand-all
        >
          <!-- <template slot-scope="obj"> -->
          <template #default="obj">
            <treeTools
              :tree-node="obj.data"
              @delDepartment="getDepartment"
              @addDepartment="addDepartment"
              @ediDepartment="ediDepartment"
            />
          </template>
        </el-tree>
      </el-card>
      <add-dept
        ref="addDept"
        :is-show-dialog.sync="isShowDialog"
        :tree-node="treeNode"
        @addDepartment="getDepartment"
      />
    </div>
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { listToTreeData } from '@/utils/index'
import treeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
export default {
  components: { treeTools, AddDept },

  data() {
    return {
      isShowDialog: false,
      treeNode: {},
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      depts: []

    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const { depts } = await getDepartment()
      this.depts = listToTreeData(depts, '')
    },
    addDepartment(treeNode) {
      this.isShowDialog = true
      this.treeNode = treeNode
    },
    async ediDepartment(treeNode) {
      await this.$refs.addDept.getDepartment(treeNode.id)
      this.isShowDialog = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
